package com.karl.springboot.controller;

import com.karl.springboot.constant.SystemConstant;
import com.karl.springboot.entity.TAdmin;
import com.karl.springboot.service.TAdminService;
import com.karl.springboot.utils.JsonUtils;
import com.karl.springboot.utils.JwtUtils;
import com.karl.springboot.utils.R;
import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;


/**
 * 
 * @author karl
 *   TAdmin控制器
 * @date 2020-12-05 15:43:02
 */
@Controller
@RequestMapping("/tadmin")
public class TAdminController extends BaseController<TAdmin>{

	@RequestMapping("login")
    @ResponseBody
    public R login(@RequestBody TAdmin tAdmin){
        try {
            TAdmin t = tAdminService.findObjectByPramas(tAdmin);
            if (t!=null){
                String jwt = JwtUtils.createJWT("1", JsonUtils.objectToJson(t), SystemConstant.JWT_TTL);
                System.out.println(jwt);
                return R.ok(jwt);
            }
            else {
                return R.error("用户名或者密码有误");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("访问出错");
        }

    }
    @GetMapping(value = "/refreshToken")
    public @ResponseBody R refreshToken(HttpServletRequest request){
        Claims claims = JwtUtils.validateJWT(request.getHeader("token")).getClaims();
        String JWT = JwtUtils.createJWT(claims.getId(),claims.getSubject(), SystemConstant.JWT_TTL);
        return R.ok(JWT);
    }

}
